package cn.broadmax.boot.modules.sys.service.impl;

import cn.broadmax.boot.modules.sys.service.ISysDictService;
import cn.broadmax.boot.modules.sys.entity.SysDict;
import cn.broadmax.boot.modules.sys.mapper.SysDictMapper;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jexx.util.StringUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author jexx
 * @since 2021-05-19
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Override
    public SysDict addDict(SysDict dict) {
        String code = StringUtil.trim(dict.getDictCode());
        SAssert.hasText(code, "字典不能为空");

        SysDict exist = getByCode(code);
        SAssert.isNull(exist, "字典编号已存在");

        return null;
    }

    @Override
    public SysDict updateDictById(String dictId, SysDict dict) {
        String code = StringUtil.trim(dict.getDictCode());
        SAssert.hasText(code, "字典不能为空");

        SysDict exist = getByCode(code);
        SAssert.isNull(exist == null || dictId.equals(exist.getId()), "字典编号已存在");

        dict.setId(dictId);
        super.updateById(dict);
        return super.getById(dictId);
    }

    @Override
    public SysDict getByCode(String dictCode) {
        if(StringUtil.isEmpty(dictCode)){
            return null;
        }
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDict::getDictCode, dictCode);
        return super.getOne(wrapper);
    }

}
